package com.itbupt.eat.common;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author BUPT-Dingchang
 * @Create 2024/4/20 21:47
 * ClassName: R
 * Description:
 */

@Data
public class R<T> {

    // 状态码--1成功，0失败
    private Integer code;
    // 错误信息
    private String msg;
    // 返回数据
    private T data;

    // 动态数据
    private Map map = new HashMap();

    public R() {
    }

    // 适用于成功
    public static <T> R<T> success(T object) {
        R<T> r = new R<>();
        r.code = 1;
        r.data = object;
        return r;
    }

    // 适用于失败
    public static <T> R<T> error(String msg) {
        R<T> r = new R<>();
        r.code = 0;
        r.msg = msg;
        return r;
    }

    // 操作动态数据的
    public R<T> add(String key, Object value) {
        this.map.put(key, value);
        return this;
    }
}
